package com.example.telesys.service.impl;

import com.example.telesys.mapper.CustomerMapper;
import com.example.telesys.pojo.Customer;
import com.example.telesys.pojo.CustomerExample;
import com.example.telesys.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public int addCustomer(String username) {
        Customer customer = new Customer();
        customer.setCustomername(username);
        customer.setUsername(username);
        return customerMapper.insert(customer);
    }

    @Override
    public int updateCustomerInfo(Customer customer) {
        CustomerExample customerExample = new CustomerExample();
        CustomerExample.Criteria criteria = customerExample.createCriteria();
        criteria.andUsernameEqualTo(customer.getUsername());
        return customerMapper.updateByExampleSelective(customer, customerExample);
    }

    @Override
    public Customer selectCustomerInfo(String username) {
        CustomerExample customerExample = new CustomerExample();
        CustomerExample.Criteria criteria = customerExample.createCriteria();
        criteria.andUsernameEqualTo(username);
        return customerMapper.selectByExample(customerExample).get(0);
    }

    @Override
    public List<Customer> selectAllCustomers() {
        return customerMapper.selectByExample(null);
    }

    @Override
    public List<Customer> selectCustomersByName(String customername) {
        CustomerExample customerExample = new CustomerExample();
        CustomerExample.Criteria criteria = customerExample.createCriteria();
        criteria.andCustomernameEqualTo(customername);
        return customerMapper.selectByExample(customerExample);
    }
}
